package com.example.mybatisannotation.mapper;

import com.example.mybatisannotation.entity.Card;
import com.example.mybatisannotation.entity.Student;
import org.apache.ibatis.annotations.*;

import javax.naming.Name;
import java.util.List;

@Mapper
public interface StuMapper {
    @Result(property = "card",column = "no",
    one=@One(select = "com.example.mybatisannotation.mapper.StuMapper.getCardByNo"))
    @Select("select * from student where name like concat ('%',#{name},'%')")
    List<Student> getUsersByName(String name);

    @Select("select * from student")
    @Results(id = "stuMap",value = {
            @Result(id = true,column = "name",property = "name"),
            @Result(id = true,column = "id",property = "id")
    })
    List<Student> getStuBynamepa(String name,String password);

    @Select("select * from card where cardNo=#{number}")
    Card getCardByNo(String number);

    @Select({
            "<script>",
            "select * from student where 1=1",
            "<if test='name!=null'>",
            "and name=#{name}",
            "</if>",
            "<if test='no!=null'>",
            "and no=#{no}",
            "</if>",
            "</script>"
    })
    List<Student> getUserByNumber(String name,String no);


    @Insert("insert into student(no,name,sex,phone,qq,id) values(#{no},#{name},#{sex},#{phone},#{qq},#{id})")
    int create(Student student);

    @Select("select * from student where name like concat('%',#{name},'%')")
    List<Student> getByName(String name);

    @Select("select * from student where id=#{id}")
    Student getByID(Integer id);

    @Delete("delete from student where id=#{id}")
    int deleteById(Integer id);

    @Select("select * from student")
    List<Student> getAllStudents();

    @Update("update student set no=#{no},name=#{name},sex=#{sex},phone=#{phone},qq=#{qq} where id=#{id}")
    int updateStu(Student stu);

    @Results(id="StuMap",value={
            @Result(property="name",column="name")
    })

    @Select("select * from user where name=#{name}")
    Student getStuByName(String name);
    @Insert("insert into student(id,no,name,sex,phone,qq) values (#{id},#{no},#{id},#{sex},#{phone},#{qq})")
    int createStu(Name name);


}
